#include <sourcemod>
#include <sdktools>

#pragma semicolon 1

// Constants
#define PLUGIN_VERSION	 "1.00"

#define MESS			 "\x03[丫蛋] \x01%t"
#define DEBUG_MESS		 "\x04[丫蛋] \x01"

// Global vars
new Float:originSaves[MAXPLAYERS+1][3];
new Float:angleSaves[MAXPLAYERS+1][3];
new Float:angleEyes[MAXPLAYERS+1][3];

public Plugin:myinfo =
{
	name = "人物坐标移动系统",
	author = "[Yd]曼陀罗",
	description = "允许进行控制台命令移动自己到相对位置",
	version = PLUGIN_VERSION,
	url = "http://www.l4d2.com/"
};

public OnPluginStart()
{
	// Console commands	
	RegConsoleCmd("sm_move_f", Command_Move_F);		// 向视觉方向移动

}

public Action:Command_Move_F(client, args)
{
	if(!IsValidPlayer(client, false, false)) 
	{
		PrintToChat(client, "[丫蛋] 非正常状态或有效玩家，不可能使用此命令!");
		return Plugin_Handled;
	}
	if(args < 1)
	{
		PrintToChat(client, "[丫蛋] 请输入移动距离");
	}
	decl String:arg[5];
	GetCmdArgString(arg, sizeof(arg));
	new Float:number = StringToFloat(arg);	
	// 获取当前坐标
	GetClientAbsOrigin(client, originSaves[client]);
	GetClientAbsAngles(client, angleSaves[client]);
    GetClientEyeAngles(client, angleEyes[client]);
	new Float:number_x = number * Cosine(angleSaves[client][1] * 3.14159 / 180.0);
	new Float:number_y = number * Sine(angleSaves[client][1] * 3.14159 / 180.0);
	new Float:number_z = -number * Sine(angleEyes[client][0] * 3.14159 / 180.0);	
    //PrintToChat(client, "玩家坐标数据 <%f, %f, %f>，角度数据<%f, %f, %f>!", originSaves[client][0], originSaves[client][1], originSaves[client][2], angleSaves[client][0], angleSaves[client][1], angleSaves[client][2]);    
    //PrintToChat(client, "视觉角度数据<%f, %f, %f>!", angleEyes[client][0], angleEyes[client][1], angleEyes[client][2]);
	//PrintToChat(client, "方向数据 %f, 玩家移动数据 <%f, %f, %f>，!", number, number_x, number_y, number_z);
	// 坐标增加
	originSaves[client][0] += number_x;
	originSaves[client][1] += number_y;
	originSaves[client][2] += number_z;
	// 移动到位置
    //PrintToChat(client, "玩家坐标数据 <%f, %f, %f>", originSaves[client][0], originSaves[client][1], originSaves[client][2]);
	TeleportEntity(client, originSaves[client], angleSaves[client], NULL_VECTOR);	
	return Plugin_Handled;
}

/* 判断玩家是否有效 */
stock bool:IsValidPlayer(Client, bool:AllowBot = true, bool:AllowDeath = true)
{
    if (Client < 1 || Client > MaxClients)
        return false;
        
    if (!IsClientConnected(Client) || !IsClientInGame(Client))
        return false;
        
    if (!AllowBot)
    {
        if (IsFakeClient(Client))
            return false;
    }
    
    if (!AllowDeath)
    {
        if (!IsPlayerAlive(Client))
            return false;
    }    
    
    return true;
}
